$(function(){
	tmLogin.loginSuccess();
	tmTop["init"]();
	tmErrol["init"]();
	tmErrol["errol"]();
});

/*损坏图片的处理方案*/
var tmErrol = {
	init:function(){
		$(function(){
			$("img").on("error",function(){
				$(this).attr("src",basePath+"/resources/images/noimage.gif");
			});
		});
	},
	errol:function(obj){
		$(obj).on("error",function(){
			$(this).attr("src",basePath+"/resource/images/noimg.jpg");
		});
	}
};
/*置顶操作*/
var tmTop = {
	init:function(){
		$(window).scroll(function(){
			var top = $(this).scrollTop();
//		 		if(top>200){
//		 			$("#tm_top").show();
//		 		}else{
//		 			$("#tm_top").hide();
//		 		}
			$("#tm_top")[top>200?"show":"hide"]();
		});
		$("#tm_top").click(function(){
			$("html,body").animate({scrollTop:0});
		});	
	}
};

/***************错误消息提示******************/
var tzMessage = {
	tip:function(message){
		$("#messagetip").remove();
		$("body").append("<div id='messagetip'>"+
		"<span><i class='iconfont icon-bell'></i>"+message+"</span>"+
		"</div>");
		$("#messagetip").animate({top:0},1000).delay(2000).animate({top:-56});
		$("#messagetip").on("click",function(){
			$(this).stop(true,true).animate({top:-56},500);
		});
	}	
};
/***************登录实现******************/
var tmLogin = {
	loginIndex:0,
	mark:false,
	logout:function(obj){
		$.ajax({
			type:"post",
			url:basePath+"/login/logout.do",
			success:function(data){
				if(data=="success"){
					window.location.href = basePath;
					var html = "<a href='javascript:void(0);' onclick='tmLogin.login()'><i class='iconfont icon-login-user'></i></a>"+
					"<a href='javascript:void(0)' onclick='tmLogin.register()'>注册</a>";
					$("#loginbox").html(html);
				}
			}
		});
	},
	loginSuccess:function(){
		$.ajax({
			type:"post",
			url:basePath+"/login/loginsuccess.do",
			success:function(data){
				var html = "";
				if(data){//登录成功  "+basePath+"/admin/index.do"+"
					 html = "<a href='javascript:void(0)' id='username' title='"+data.username+"'><img class='user-head' src='"+basePath+"/"+data.headerPic+"'/></a>"+
					 "<ul id='user_menu'>"+
					 "	<li class='u-items'><i class='iconfont icon-guanbi fz16'></i><a href='"+basePath+"/personal/article.do"+"' target='_blank'>我的文章</a></li>"+
					 "	<li class='u-items'><i class='iconfont icon-guanbi fz16'></i><a href='"+basePath+"/personal/article.do"+"' target='_blank'>我的音乐</a></li>"+
					 "	<li class='u-items'><i class='iconfont icon-guanbi fz16'></i><a href='"+basePath+"/personal/article.do"+"' target='_blank'>帐号设置</a></li>"+
					 "	<li class='u-items'><i class='iconfont icon-guanbi fz16'></i><a href='"+basePath+"/personal/article.do"+"' target='_blank'>备份</a></li>"+
					 "	<li class='u-items'><i class='iconfont icon-guanbi fz16'></i><a class='tmlogout' href='javascript:void(0);' onclick='tmLogin.logout(this)'>退出</a></li>"+
					 "</ul>";
				}else{
					 html = "<a href='javascript:void(0);' onclick='tmLogin.login()'><i class='iconfont icon-login-user'></i></a>"+
					"<a href='#'>注册</a>";
				}
				$("#userbox").html(html);
			}
		});
	},
	changeCode:function(img){
		img.src = basePath+'/kaptcha/code.do?d='+new Date().getTime();
	},
	login:function(){
		$("#dialog_login").remove();
		$("body").append(
				"<div id='dialog_login'>"+
				"	<div class='loginbox'>"+
				"		<div class='logo'><a href='javascript:void(0)'><img src='"+basePath+"/resources/images/logo.png' width='200' height='50'></a></div>"+
				"		<div class='cnt_p'>"+
				"			<p><input type='text' class='ipt' name='email' id='email' placeholder='请输入帐号/邮箱...' value='18093454447@163.com'></p>"+
				"			<p><input type='password' class='ipt' name='password' id='password' placeholder='请输入密码...' value='123456'></p>"+
				"			<p id='kcode' style='position:relative;display:none;'><input type='text' class='ipt' name='code' maxlength='4' id='code' placeholder='验证码...'><img src='"+basePath+"/kaptcha/code.do' onclick='tmLogin.changeCode(this);' style='position: absolute;top:1px;right:28px;'/></p>"+
				"			<p><a href='javascript:void(0)' class='subbtn'><i></i>登录</a></p>"+
				"		</div>"+
				"	</div>"+
				"</div>"+
				"<div class='dialogover'></div>");
		/********点击其他区域取消弹窗登录********/
		$("#dialog_login").next().off("click").on("click",function(){
			$("#dialog_login").add(this).fadeOut("slow",function(){
				$(this).remove();
			});
		});
		/********登录的基本验证********/
		//给登录按钮绑定点击事件
		$("#dialog_login").find(".subbtn").off("click").on("click",loginMain);
		//点击的主方法
		function loginMain(){
			var $btn = $(this);
			var email = $("#email").val();
			var password = $("#password").val();
			var code = $("#code").val();
			//错误输入的次数为3时候.显示验证码
	  		if(tmLogin.loginIndex>=3){
	  			$("#kcode").show();
	  			tmLogin.mark = true;
	  		}
			if(isEmpty(email)){
				tzMessage.tip("请输入账号...");
				tmLogin.loginIndex++;
				$("#email").focus();
				return;
			}
			if(isEmpty(password)){
				tzMessage.tip("请输入密码...");
				tmLogin.loginIndex++;
				$("#password").focus();
				return;
			}
	  		if(tmLogin.mark && isEmpty(code)){
	  			tzMessage.tip("请输入验证码...");
	  			$("#code").focus();
	  			return;
	  		}
			//传递给服务器端的数据	
			var params ={"email":email,"password":password,"code":code};
			/********登录的ajax提交********/
			//杀掉当前点击事件
			$btn.off("click").text("登录中...").css({"background":"rgba(255, 192, 203, 0.88)","color":"#54504c"});
			$.ajax({
				type:"post",
				url:basePath+"/login/logined.do",
				data:params,
				error:function(){
					$btn.on("click",loginMain).text("登录").css({"background":"rgba(204, 204, 204, 0.82)","color":"rgba(0, 128, 0, 0.8)"});
				},
				success:function(data){
					$btn.on("click",loginMain).text("登录").css({"background":"rgba(204, 204, 204, 0.82)","color":"rgba(0, 128, 0, 0.8)"});
					/*if(data=="success"){
						$("#dialog_login").next().trigger("click");
						//更改登陆的用户信息
					}*/
					if(data=="emial_null"){
						tzMessage.tip("请输入账号...");
						tmLogin.loginIndex++;
						$("#email").focus();
						return;
					}else if(data=="password_null"){
						tzMessage.tip("请输入密码...");
						tmLogin.loginIndex++;
						$("#password").focus();
						return;
					}else if(data=="code_error"){
						tzMessage.tip("验证码错误...");
						$("#code").select();
						return;
					}else if(data=="fail"){
						tzMessage.tip("帐号和密码有误...");
						tmLogin.loginIndex++;
						$("#email").select();
						$("#password").val("");
						return;
					}else{
						tzMessage.tip("登录成功！");
						tmLogin.loginIndex = 0;
						tmLogin.loginSuccess();
						$("#dialog_login").next().trigger("click");
					}
				}
			});
		};
	},
	//显示用户菜单信息
	/*showmenu:function(obj){
		var mark = $(obj).attr("mark");
		$("#userbox").css("overflow",mark==1?"visible":"hidden");
		$(obj).attr("mark",mark==1?0:1);
	}*/
};